using System;
using System.Collections.Generic;
using System.Linq;
namespace TemplateWeb.Models.Repositories
{
	#region using
	using TemplateWeb.Helpers;
	using TemplateWeb.Models.ViewModels;
	using TemplateWeb.Models;
	#endregion
	public class DanhMucWebRepository
	{
		public List<DanhMucWeb> SelectAll()
		{
			try
			{
				return DataContext.Instance.DanhMucWeb.ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("DanhMucWeb - SelectAll: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("DanhMucWeb - SelectAll: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return new List<DanhMucWeb>();
			}
		}

		public DanhMucWeb SelectByID(int id)
		{
			try
			{
				return DataContext.Instance.DanhMucWeb.FirstOrDefault(item => item.ID == id );
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("DanhMucWeb - SelectByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("DanhMucWeb - SelectByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return null;
			}
		}

		public List<DanhMucWeb> SelectPaging(int page, int pageSize)
		{
			try
			{
				var lstItem = DataContext.Instance.DanhMucWeb.ToList();
				if (pageSize > 0)
				{
					int startIndex, itemCount;
					startIndex = (page - 1) * pageSize;
					itemCount = pageSize;
					if (startIndex + itemCount > lstItem.Count)
						itemCount = lstItem.Count - startIndex;
					lstItem = lstItem.GetRange(startIndex, itemCount);
				}
				return lstItem;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("DanhMucWeb - SelectPaging: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("DanhMucWeb - SelectPaging: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return new List<DanhMucWeb>();
			}
		}

		public bool Insert(DanhMucWeb obj)
		{
			try
			{
				DataContext.Instance.DanhMucWeb.AddObject(obj);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("DanhMucWeb - Insert: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("DanhMucWeb - Insert: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return false;
			}
		}

		public bool Delete(int id)
		{
			try
			{
				var delitem = DataContext.Instance.DanhMucWeb.FirstOrDefault(item => item.ID == id );
				DataContext.Instance.DanhMucWeb.DeleteObject(delitem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("DanhMucWeb - Delete: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("DanhMucWeb - Delete: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return false;
			}
		}

		public bool Save()
		{
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch
			{
				return false;
			}
		}

		public int GetTotalItem()
		{
			try
			{
				return DataContext.Instance.DanhMucWeb.Count();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("DanhMucWeb - GetTotalItem: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("DanhMucWeb - GetTotalItem: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return -1;
			}
		}


		public int GetTotalPage()
		{
			int RecordCount = GetTotalItem();
			int PageSize = WebConfiguration.NumDanhMucWebPerPage;
			return (RecordCount / PageSize) + ((RecordCount % PageSize == 0) ? 0 : 1);
		}


		public List<DanhMucWeb> RetrieveByID(int id)
		{
			try
			{
				return (from item in DataContext.Instance.DanhMucWeb where  item.ID == id  select item).ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException != null)
					CommonLogging.Logger.Error("DanhMucWeb - RetrieveByID: " + ex.InnerException.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				else
					CommonLogging.Logger.Error("DanhMucWeb - RetrieveByID: " + ex.Message.Replace("\t", string.Empty).Replace("\r", string.Empty).Replace("\n", string.Empty));
				return new List<DanhMucWeb>();
			}
		}

	}
}